﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Filter
{
    /// <summary>
    /// 提供了在表现上为Enum，在数据库中为int(或short)类型的字段的过滤器。
    /// </summary>
    /// <typeparam name="T">必需为枚举相关类型，并可以直接将其值转换为int值。</typeparam>
    public class EnumFilterField<T> : FilterField
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public EnumFilterField(T value)
            :this(value, SqlOperations.Equals)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="operation"></param>
        public EnumFilterField(T value, SqlOperations operation)
        {
            if (!typeof(T).IsSubclassOf(typeof(Enum)))
            {
                throw new ArgumentException("T can only be sub type of enum.");
            }

            int ivalue = Convert.ToInt32(value);
            field = new IntegerFilterField(ivalue, operation);
        }

        private IntegerFilterField field;

        /// <summary>
        /// 
        /// </summary>
        public T Value
        {
            get { return (T)Enum.ToObject(typeof(T), field.Value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public SqlOperations Operation
        {
            get { return field.Operation; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return field.ToString();
        }
    }
}
